package com.gs.auth.service.impl;

import com.gs.auth.mapper.UserMapper;
import com.gs.common.core.redis.RedisCache;
import com.gs.common.enums.ExceptionCode;
import com.gs.common.exception.CustomException;
import com.gs.common.utils.StringUtils;
import com.gs.common.utils.sign.Md5Utils;
import com.gs.auth.po.TbUser;
import com.gs.auth.vo.UserVo;
import com.gs.auth.service.UserService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private RedisCache redisCache;
    @Resource
    private UserMapper userMapper;
    @Override
    public TbUser selectUser(String userAccount,String password) {
        if(StringUtils.isEmpty(userAccount)&&StringUtils.isEmpty(password)){
            return null;
        }else{
            TbUser tbUser = userMapper.selectUserByUserAccount(userAccount);
            if(tbUser!=null && tbUser.getUserPassword().equals(password)){
                return tbUser;
            }else{
                return null;
            }
        }
    }

    @Override
    public void queryCode(String phone) {
        //redis存储，只允许发三次
        long count = redisTemplate.opsForValue().increment("user_regist_time"+phone);
        if(count>3){
            redisTemplate.expire("user_regist_time"+phone,24, TimeUnit.HOURS);
            throw new CustomException(ExceptionCode.CELL_PHONE_VERIFY_CODE_LIMIT);
        }

        long timeMillis = System.currentTimeMillis();
        String code = (timeMillis+"").substring(9);
        Map<String,String> map = new HashMap<>();
        map.put("phone",phone);
        map.put("code",code);
        //mq生成到队列中
        rabbitTemplate.convertAndSend(
                "gs.phone.code.exchange",
                "phone.code",
                map
        );

    }

    @Override
    public void register(UserVo userVo) {
        System.out.println(userVo);
        TbUser tbUser = userMapper.selectUserByUserAccount(userVo.getUserPhone());
        if(Objects.nonNull(tbUser)){
            throw new CustomException(ExceptionCode.USER_ACCOUNT_IN_USE);
        }
        String code =redisCache.getCacheObject("user_regist_phone_code"+userVo.getUserPhone());
        if(!StringUtils.equals(code,userVo.getCode())){
            throw new CustomException(ExceptionCode.INVALID_KAPTCHA);
        }
        TbUser user = new TbUser();
        user.setUserAccount(userVo.getUserPhone());
        user.setUserName(userVo.getUserPhone());
        user.setUserPhone(userVo.getUserPhone());
        user.setUserType(2);
        user.setUserStatus(1);
        user.setUserPassword(Md5Utils.hash(Md5Utils.hash(userVo.getUserPassword())));
        userMapper.insert(user);
    }

    @Override
    public TbUser getUserInfo(Long userId) {
        return userMapper.getUserInfo(userId);
    }


}
